import { defHttp } from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
	list = '/activity/bsActiveItem/list',
	save = '/activity/bsActiveItem/add',
	activityListapi = '/activity/bsActiveItem/getActivityTypesByRedis',
	edit = '/activity/bsActiveItem/edit',
	deleteOne = '/activity/bsActiveItem/delete',
	deleteBatch = '/activity/bsActiveItem/deleteBatch',
	importExcel = '/activity/bsActiveItem/importExcel',
	exportXls = '/activity/bsActiveItem/exportXls',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
	defHttp.get({ url: Api.list, params });


export const activityList = async (params) => {
	let types = defHttp.get({ url: Api.activityListapi, params })
	let o = JSON.parse(await types);
	return o;
}


/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
	return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
		handleSuccess();
	});
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
	createConfirm({
		iconType: 'warning',
		title: '确认删除',
		content: '是否删除选中数据',
		okText: '确认',
		cancelText: '取消',
		onOk: () => {
			return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
				handleSuccess();
			});
		}
	});
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
	let titlegroup = {
		secondTitle: params.secondTitle,
		mainTitle: params.mainTitle
	}
	params.titlegroup = JSON.stringify(titlegroup)
	let extInfors = {
		activityTypes: params.activityTypes,
		activityTypesName: params.typeAcitvity,
		tags: params.tags,
		startCityId: params.cityId || '',
		startCityName: '',
		cityId:params.cityId || '',
		startdateStr:params.startdateStr || '',
		travelDays:params.travelDays || '',
		destination:params.destination || ''
	}
	params.extinfors = JSON.stringify(extInfors)
	params.startdate = new Date(params.startdate).getTime();
	params.enddate = new Date(params.enddate).getTime();
	
	if (params.priceexpiretime) {
		params.priceexpiretime = new Date(params.priceexpiretime).getTime();
	} else {
		params.priceexpiretime = 0
		params.activeprice = 0
	}
	if (isUpdate) {
		params.modifytime = new Date(params.modifytime).getTime();
	} else {
		params.modifytime = 0
	}

	if (!isUpdate) {
		//params.images = JSON.stringify(params.images.split(","))
	}
	params.images=params.image.split(",")+""



	let url = isUpdate ? Api.edit : Api.save;
	return defHttp.post({ url: url, params });
}